<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
         function ajax(url, data, callback, method = 'get') {
            let x = new XMLHttpRequest();
            let arr = Object.keys(data);
            let newArr = arr.map(item => `${item}=${data[item]}`);
            let str = newArr.join('&');
            if (method === 'get') {
                x.open(method, url + '?' + str);
                x.send();
            }else{
                x.open(method,url);
                x.setRequestHeader('Content-type','application/x-www-form-urlencoded')
                x.setRequestHeader('a','123');
                x.send(str)
            }

           
            x.onreadystatechange = function () {
                if (x.readyState === 4) {
                    if (x.status >= 200 && x.status < 300) {

                        console.log(x.getAllResponseHeaders());

                        let obj = x.response;
                        callback(obj)
                    }
                }
            }
        }

        ajax('http://localhost/postServer', { name: "laowang", age: 18 }, function (data) {
            console.log(data);
        }, 'post')


    </script>
</body>
</html>